/**
 * USimpleTabStrip is a simple Unobtrusive Javascript that transform a standard
 * anchor link in an animate tabstripping effect.
 * You need prototype.js and scriptaculous.js
 *
 * @author		Giovambattista Fazioli
 * @email		g.fazioli@undolog.com
 * @web			http://www.undolog.com
 * @version		1.0.2
 *
 */
var USimpleTabStrip = Class.create();
USimpleTabStrip.prototype = {
	/**
	 * Class (prototype.js) constructor
	 */
	initialize: function() {
		this.__version 		= "1.0.2";		// version	
		this.selected;						// last selected tab
		this.onHide			= undefined;	// callback
		this.onShow			= undefined;	// callback
	},
	
	/**
	 * Init the patch
	 */
	Init: function() {
		var _uts = this;
		$$('a[href^=#]:not([href=#])').each(
			function(e) {
				e.observe('click',
					function(e) {
						var c = 'a[name="'+this.hash.substr(1)+'"]';
						$$(c).each(
							function(e) {
								if( _uts.selected != e.next() ) {
									if( _uts.onHide == undefined) _uts.selected.hide(); else _uts.onHide( _uts.selected );
									_uts.selected = e.next();
									if( _uts.onShow == undefined) _uts.selected.show(); else _uts.onShow( _uts.selected );
								}
							}
						);
					}
				);
			}
		);
		this.showFirstTab();
	}, 
	
	/**
	 * Show only the first pseudo-Tabs
	 *
	 * @private
	 */
	showFirstTab: function() {
		var _uts = this;
		var i = 0;
		$$('a[name])').each(
			function(e) {
				if(i>0) e.next().style.display="none"; else { i++; _uts.selected = e.next(); _uts.selected.show(); }
			}
		);
	},
	
	/**
	 * Find all A elements with attribute name and set display style
	 * to the next element of A.
	 * For example see the code below:
	 *
	 * <a name="abc"></a>
	 * <p>Hello</p>
	 *
	 * The element P is show or hide 
	 *
	 * @param		v		(Boolean) If true next element is show, alse is hide
	 * @return		void
	 */
	showTabs: function(v) {
		$$('a[name])').each(
			function(element){
				element.next().style.display=v?'':'none';
			}
		);
	}, 
	/**
	 * Show alert version
	 *
	 * @param		void
	 * @return		open alert for show the version
	 */
	version: function() {
		alert( this.__version );
	}	
};